import Vue from 'vue'
import VueRouter from 'vue-router'
import * as path from "path";


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/home',

    component: () => import('@/views/Home'),
    children: [
        {path:"img",component:()=> import("@/components/HomePage"),meta:{title:'欢迎'},},
        {path: "shop",component:()=> import("@/components/Shop"),meta:{title:'店铺管理'},},
        {path: "commodity",component:()=> import("@/components/Product"),meta:{title:'商品管理'},},
        {path: "users",component:()=> import("@/components/Grouper"),meta:{title:'团长管理'},},
        {path: "order",component:()=> import("@/components/Order"),meta:{title:'订单管理'},},
        {path: "audit",component:()=> import("@/components/Audit"),meta:{title:'店铺审核'},},
        {path: "deposit",component:()=> import("@/components/Deposit"),meta:{title:'店铺余额'},},
        {path: "type",component:()=> import("@/components/Type")
        },
    ]
  },
    {
        path: '/store',
        component: () => import('@/views/Store'),
        children: [
            {path:"details",component:()=> import("@/components/Details"),meta:{title:'店铺详情'},},
            {path:"storeProduct",component:()=> import("@/components/StoreProduct"),meta:{title:'商品管理'},},
            {path:"storeOrder",component:()=> import("@/components/StoreOrder"),meta:{title:'订单管理'},},
        ]
    },
  {
    path: '/login',
    meta:{title:'登入'},
    component: () => import('@/views/Login'),
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(
    (to, from, next) => {
        if (to.meta.title) {
            document.title = to.meta.title
        }
        if(localStorage.getItem("token") == null){
            if(to.path == "/login"){
                next()
            }else {
                router.push('/login')
            }
        }else {
            //storeId 店铺
            //community 社区
            next()
        }
        if(to.path == "/login"){
            if(localStorage.getItem("community")!=null){
                router.push('/home/shop')
            }else if(localStorage.getItem("storeId")!=null){
                router.push('/store/details')
            }
        }
    })

export default router
